package ignite.controller;

import ignite.service.PersonService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ignite.entity.Person;
import org.springframework.web.bind.annotation.*;

import javax.cache.Cache;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping("/person")
public class PersonController {
    private PersonService personService;

    @GetMapping("/getByName/{firstName}")
    public List<Person> getByName(@PathVariable("firstName") String firstName) {
        List<Cache.Entry<Integer, Person>> entryList = personService.findByFirstName(firstName);

        return entryList.stream().map(Cache.Entry::getValue).collect(Collectors.toList());
    }

    @GetMapping("/getById/{id}")
    public Person getById(@PathVariable("id") Integer id) {
        Person person = personService.selectPersonById(id);
        return person;
    }

    @GetMapping("/add")
    public Person add(@RequestBody Person person) {
        personService.save(person);
        return person;
    }
}
